import $ from 'jquery';
import hex_md5 from '../js/md5';
import { baseUrl } from '../js/tajax';

export function signIn(redirect) {
    let value = document.getElementById('login-type-select').value;
    let phone = document.getElementById('login-phone').value;
    let password = document.getElementById('login-password').value;

    let type = 'C';
    switch (value) {
        case '我是顾客':
            type = 'C';
            break;
        case '我是商家':
            type = 'B';
            break;
        case '我是配送员':
            type = 'D';
            break
    }

    $.ajax({
        url: baseUrl + '/auth/sign_in',
        method: 'post',
        data: {
            phone: type + phone,
            password: hex_md5(password)
        },
        crossDomain: true,
        withCrednetial: true,
        success: function (data, code, xhr) {
            if (data.code === 0) {
                localStorage.setItem('user-type', data.data.type);
                localStorage.setItem('user-id', data.data.id);
                localStorage.setItem('displayName', data.data.displayName);
                localStorage.setItem('Token', xhr.getResponseHeader('Token'));
                window.location.href = redirect == null ? '/' : redirect;
            } else {
                alert(data.message);
            }
        },
        error: function (data) {
            alert(data.message);
        }
    });
}


export function signUpAsCustomer(displayName, phone, password) {
    if (!displayName || !phone || !password) {
        alert('请填写完成后提交');
        return;
    }
    $.ajax({
        url: baseUrl + '/auth/sign_up/customer',
        method: 'post',
        data: {
            phone: phone,
            password: hex_md5(password),
            displayName: displayName
        },
        crossDomain: true,
        success: function (data) {
            alert(data.message);
            if (data.code == 0) {
                window.location.href = '/login';
            }
        },
        error: function (data) {
            alert(data.message);
        }
    });
}

export function signUpAsBusiness(phone, password) {
    if (!phone || !password) {
        alert('请填写完成后提交');
        return;
    }
    $.ajax({
        url: baseUrl + '/auth/sign_up/business',
        method: 'post',
        data: {
            phone: phone,
            password: hex_md5(password)
        },
        crossDomain: true,
        success: function (data) {
            alert(data.message);
            if (data.code == 0) {
                window.location.href = '/login';
            }
        },
        error: function (data) {
            alert(data.message);
        }
    });
}

export function signUpAsDeliverer(phone, password, name, healthId) {
    if (!name || !phone || !password || !healthId) {
        alert('请填写完成后提交');
        return;
    }
    $.ajax({
        url: baseUrl + '/auth/sign_up/deliverer',
        method: 'post',
        data: {
            phone: phone,
            password: hex_md5(password),
            name: name,
            healthId: healthId
        },
        crossDomain: true,
        success: function (data) {
            alert(data.message);
            if (data.code == 0) {
                window.location.href = '/login';
            }
        },
        error: function (data) {
            alert(data.message);
        }
    });
}

export default function isLogin() {
    return localStorage.getItem('Token') != null;
}

export function logout() {
    localStorage.removeItem('user-type');
    localStorage.removeItem('user-id');
    localStorage.removeItem('Token');
    localStorage.removeItem('displayName');
    window.location.href = window.location.href;
}